package calculator;

import java.util.Scanner;

/**
 * 使用计算器为案例，使用 简单工厂设计模式 实现.
 *
 * @author GongXincheng
 * @since 2019-09-13 13:52
 */
public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.print("input first number：");
        double numLeft = sc.nextDouble();

        System.out.print("input operation(+ - * /)：");
        String operationString = sc.next().trim();

        System.out.print("input next number：");
        double numRight = sc.nextDouble();

        try {
            double result = process(numLeft, numRight, operationString);
            showResult(result);
        } catch (Exception e) {
            System.out.println("Error：" + e.getMessage());
        }

    }

    /**
     * 计算
     */
    private static double process(double numLeft, double numRight, String operationString){
        AbstractOperation operation = OperationFactory.getOperation(operationString);
        operation.setLeftNum(numLeft);
        operation.setRightNum(numRight);
        return operation.getResult();
    }

    /**
     * 显示结果.
     */
    private static void showResult(double result) {
        System.out.println("Result：" + result);
    }


}
